<?php

namespace app\public_module\controller;

use app\common\controller\Base;

class Rule extends Base
{
    /*
     * 显示管理制度的页面
     */
    public function show_rule($department_id)
    {
        if ($department_id == 1) {
            $now_rule_name['name'] = '公司行政';
        } else {
            $now_rule_name = db('department')->where('id', $department_id)->field('name')->find();
        }
        $rule_list = db('rule')->where(['department_id' => $department_id, 'status' => 1])->order('id', 'desc')->select();
        $this->assign(['now_name' => $now_rule_name['name'], 'department_id' => $department_id, 'rule_list' => $rule_list]);
        return view();
    }

    /*
     * 显示添加和修改管理制度的页面
     */
    public function add_rule()
    {
        $rule_id = input('rule_id');
        $department_id = input('department_id');
        $now_name = input('now_name');
        if ($rule_id) {
            $rule_list = db('rule')->where('id', $rule_id)->find();
            $this->assign(['now_name' => $now_name, 'department_id' => $department_id, 'rule_list' => $rule_list]);
            return view("show_edit");
        } else {
            $this->assign(['now_name' => $now_name, 'department_id' => $department_id]);
            return view();
        }
    }

    /*
     * 确认提交管理制度
     */
    public function confirm_rule()
    {
        $department_id = input('department_id');
        $title = trim(input('title'));
        $content = trim(input('rule_content'));
        $info = [
            'department_id' => $department_id
            , 'title' => $title
            , 'content' => $content
        ];
        $res = db('rule')->insert($info);
        return $res;
    }

    /*
     * 确认修改管理制度
     */
    public function change_rule()
    {
        $rule_id = input('rule_id');
        $title = trim(input('title'));
        $content = trim(input('rule_content'));
        $info = [
            'title' => $title
            , 'content' => $content
        ];
        $res = db('rule')->where('id', $rule_id)->update($info);
        return $res;
    }

    /*
     * 删除管理制度
     */
    public function delete_rule()
    {
        $now_id = input('id');
        $res = db('rule')->where('id', $now_id)->update(['status' => 0]);
        return $res;
    }
}